import java.lang.reflect.Array;
import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description：
 * User：user
 * DATE:2021-11-09
 * Time:23:15
 */
public class TestPriority {

    public  int[] elem;
    public  int usedSize;
    public  TestPriority(){
        this.elem = new int[10];
    }
    public  void shiftDown(int parent,int len){
        int child = 2 * parent + 1;
        while (child < len){
            //父节点和两个结点中的最大值进行交换
            //首先找到两个结点中的最大值
            //此处需要先判断是否有右节点
            if(child + 1 < len && this.elem[child] < this.elem[child + 1]){
                child++;//此时移动表示右节点是两个结点中的较大值，否则不移动表示左节点是做大值
            }
            //下来比较父节点和子节点中的较大值之间的关系
            if(this.elem[child] > this.elem[parent]){
                int tmp = this.elem[child];
                this.elem[child] =  this.elem[parent];
                this.elem[parent] = tmp;
                //可能有多层结点，需要进行多次tiaozh
                parent = child;
                child =  2 * parent + 1;
            } else{
                break;
            }
        }
    }


    public  void  createTree(int[] array){
        for (int i = 0; i < array.length; i++) {
            this.elem[i]  = array[i];
            this.usedSize++;
        }
        //建堆向下调整每棵子树
        //首先是找到最后一棵子树，向上寻找到root
        for (int parent = (this.elem.length - 1 - 1) / 2; parent >= 0; parent--) {
            shiftDown(parent,this.elem.length);
        }

    }
    public  void shiftUp(int child,int len){
        int parent = (child - 1) / 2;
        while (child >= 0){
            if(this.elem[child] > this.elem[parent]){
                int tmp = this.elem[child];
                this.elem[child] = this.elem[parent];
                this.elem[parent] = tmp;
                child = parent;
                parent = (child - 1 ) / 2;
            }else{
                break;
            }
        }

    }

    public  boolean isFull(){
        return  this.elem.length == this.usedSize;
    }
    public  void push(int val){
        if(isFull()){
            this.elem = Arrays.copyOf(this.elem, 2 * this.elem.length);
        }
        this.elem[usedSize] = val;
        usedSize++;
        shiftUp(usedSize - 1,this.elem.length);
    }


    public int pop(){
        if(isEmpty()){
            return  -1;
        }
        int tmp = this.elem[0];
        this.elem[0] = this.elem[this.usedSize - 1];
        this.elem[this.usedSize - 1] = tmp;
        this.usedSize--;
        shiftDown(0,this.usedSize);
        return  tmp;
    }

    public  boolean isEmpty(){
        return  this.elem.length == 0;
    }

    public int  peek(){
        if(isEmpty())return -1;
        return  this.elem[0];
    }
}
